#--------- Date:2017-7-7 ---------
#Owner: baoshan.zhang
#Summary:Class used to fetch fund's estimation data

import pymssql
import pandas as pd
import pandas.io.sql as sql
from pandas import *
from datetime import date,datetime,timedelta
from lib.fundInfo import *
import math
import enum

class FundEstimation:
    __fundid=-1

    def __init__(self,fundId):
        self.__fundid = fundId
        conn = pymssql.connect(server='.', user='sa', password='qweqwe', database='EverBrightDB')
        query = 'SELECT FundID,AsOfDate,NetValuePerUnit,NetValue,AccruedNetValuePerUnit,MarketValue FROM ..FundEstimation WHERE FundID=%d' % fundId
        self.DailyNetValueDF = sql.read_sql(query,conn,index_col=['AsOfDate'],parse_dates=['AsOfDate'])
        self.DailyNetValueDF = self.DailyNetValueDF.dropna(subset=['NetValuePerUnit'])
        self.DailyNetValueDF['LogReturn'] = np.log(self.DailyNetValueDF['AccruedNetValuePerUnit']/self.DailyNetValueDF['AccruedNetValuePerUnit'].shift(1))

    def __get_motocarlo_paths(self,S0,M,I,R,sigma,T=1.0):
        S = np.zeros((M+1,I))
        S[0]=S0
        dt = T/M
        for t in range(1,M+1):
            z= np.random.standard_normal(I)
            S[t] = S[t-1]*np.exp((R-0.5*sigma**2)*dt + sigma * math.sqrt(dt)*z)
        return S

    def get_var(self):
        df = self.DailyNetValueDF
        log_return_mean = df.LogReturn.mean()
        normal_return = df.LogReturn


